package com.dt.web.excel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.dt.system.domain.ModRepositories;
import com.dt.system.domain.ModRepositoriesShelves;
import com.dt.system.service.IModRepositoriesService;
import com.dt.system.service.IModRepositoriesShelvesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 直接用map接收数据
 *
 * @author yxg
 */
@Component
public class RepositoriesShelvesDataListener extends AnalysisEventListener<Map<Integer, String>> {


    private static IModRepositoriesShelvesService repositoriesShelvesService;
    private static IModRepositoriesService repositoriesService;


    @Autowired
    public void setService(IModRepositoriesService repositoriesService){
        RepositoriesShelvesDataListener.repositoriesService=repositoriesService;
    }

    @Autowired
    public void setService(IModRepositoriesShelvesService repositoriesShelvesService){
        RepositoriesShelvesDataListener.repositoriesShelvesService=repositoriesShelvesService;
    }

    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 5;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) {
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData();
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        saveData();
        System.out.println("所有数据解析完成！");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {


        List<ModRepositoriesShelves> dataList=new ArrayList<>();
        //遍历所有行数
        for (Map<Integer,String> item: cachedDataList) {
            //创建对象
            ModRepositoriesShelves modRepositoriesShelves=new ModRepositoriesShelves();
            modRepositoriesShelves.setShelvesNo(item.get(3));
            modRepositoriesShelves.setLayerNum(Long.parseLong(item.get(4)));
            modRepositoriesShelves.setBoxNum(Long.parseLong(item.get(5)));
            modRepositoriesShelves.setLayerUseNum(Long.parseLong(item.get(6)));
            modRepositoriesShelves.setFileUseNum(Long.parseLong(item.get(7)));

            //查询该库架所属的库房id
            ModRepositories mr=new ModRepositories();
            mr.setRepositoriesNo(item.get(2));
            List<ModRepositories> lst=repositoriesService.selectModRepositoriesList(mr);

            if(!lst.isEmpty())
            {
                modRepositoriesShelves.setRepositoriesId(lst.get(0).getId());
            }


            //查询数据是否存在，如果不存在，则新增，存在则忽略
            ModRepositoriesShelves temp=new ModRepositoriesShelves();
            temp.setShelvesNo(item.get(3));
            List<ModRepositoriesShelves> rslst = repositoriesShelvesService.selectModRepositoriesShelvesList(temp);
            if(rslst==null || rslst.isEmpty())
            {
                dataList.add(modRepositoriesShelves);
            }
        }


        if(!dataList.isEmpty()){
            repositoriesShelvesService.batchInsertModRepositoriesShelves(dataList);
        }

    }
}